package com.settlement.system.service.impl;

import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblRrOtherCapitalDetailMapper;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.OtherCapitalStatisticsParam;
import com.settlement.system.model.entity.common.TblNcVurcherDetailPlus;
import com.settlement.system.model.entity.common.TblRrOtherCapitalDetail;
import com.settlement.system.service.ITblRrOtherCapitalDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 收退款-其他货币资金日记账 服务实现类
 *
 * @author Ximi
 * @since 2020-07-14
 */

@Slf4j
@Service
public class TblRrOtherCapitalDetailServiceImpl extends BaseServiceImpl implements ITblRrOtherCapitalDetailService {

    @Resource
    private TblRrOtherCapitalDetailMapper tblRrOtherCapitalDetailMapper;

    @Override
    public BaseMapper init() {
        return tblRrOtherCapitalDetailMapper;
    }


    @Override
    public Boolean isExistBeginBalace(TblRrOtherCapitalDetail param) throws ServiceException {
        try {
            return tblRrOtherCapitalDetailMapper.isExistBeginBalace(param);
        } catch (Exception ex){
            throw new ServiceException("是否存在期初异常",ex);
        }
    }

    @Override
    public String getLeastCapitalMonth(TblRrOtherCapitalDetail param) throws ServiceException {
        try {
            return tblRrOtherCapitalDetailMapper.getLeastCapitalMonth(param);
        } catch (Exception ex){
            throw new ServiceException("获取最小的月份",ex);
        }
    }

    @Override
    public String getLeastBeginBalaceCapitalMonth(TblRrOtherCapitalDetail param) throws ServiceException {
        try {
            return tblRrOtherCapitalDetailMapper.getLeastBeginBalaceCapitalMonth(param);
        } catch (Exception ex){
            throw new ServiceException("获取期初余额最小的月份",ex);
        }
    }


    @Override
    public void saveBeginBalance(List<TblRrOtherCapitalDetail> list) throws ServiceException {
        try {
            for (TblRrOtherCapitalDetail info:list) {
                if(tblRrOtherCapitalDetailMapper.isExist(info)){
                    tblRrOtherCapitalDetailMapper.updateBeginBalance(info);
                }else {
                    tblRrOtherCapitalDetailMapper.insert(info);
                }
            }
        } catch (Exception ex){
            throw new ServiceException("保持期初异常",ex);
        }
    }

    @Override
    public void savePageRemainAmount(List<TblRrOtherCapitalDetail> list) throws ServiceException {
        try {
            for (TblRrOtherCapitalDetail info:list) {
                tblRrOtherCapitalDetailMapper.upatePageRemainAmount(info);
            }
        } catch (Exception ex){
            throw new ServiceException("保持页面余额异常",ex);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public <ModelType> Integer saveNoDuplicateList(List<ModelType> list) throws ServiceException, DuplicateKeyException {
        for(ModelType info:list){
            if(tblRrOtherCapitalDetailMapper.isExist((TblRrOtherCapitalDetail) info)){
                tblRrOtherCapitalDetailMapper.upatePageRemainAmount((TblRrOtherCapitalDetail) info);
            }else {
                tblRrOtherCapitalDetailMapper.insert(info);
            }
        }
        return list.size();
    }

    @Override
    public TblRrOtherCapitalDetail getFoot(Map<String, Object> params) throws ServiceException {
        try {
            return tblRrOtherCapitalDetailMapper.getFoot(params);
        } catch (Exception ex){
            log.error("获取合计异常",ex);
            throw new ServiceException("获取合计异常",ex);
        }
    }
}

